package com.vivo.Utils;

import com.sun.tools.javac.util.Convert;
import com.thinkgem.jeesite.common.security.Digests;
import com.thinkgem.jeesite.common.utils.Encodes;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class SysUtils {
//
//    public static String CreateSalt(int size) {
//        byte[] buffer= new byte[size];
//
//
//    }


    /**
     * 生成盐
     * @return
     */
    public static String createSalt(){
        byte[] salt = new byte[15];
        try {
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.nextBytes(salt);

        } catch (NoSuchAlgorithmException e) {
            return null;
        }

//        System.out.println(Convert.);

        String saltStr = new sun.misc.BASE64Encoder().encode(salt).toUpperCase();
        System.out.println(saltStr);

        return saltStr;
    }


    //Base64 Decoded
//    byte[] decoded = Base64.getDecoder().decode(encoded);

    //Verify original content
//        System.out.println( new String(decoded) );


//    public static String getSysPwd(String password) {
//        String salt = SysUtils.createSalt();
//        String hashPwd = SysUtils.getPwdBySalt(password, salt);
//        return hashPwd;
//    }


    public static String getPwdBySalt(String password, String salt) {
        String needDig = salt + password;
        byte[] hashPwd = Digests.md5(needDig.getBytes());
        String hashPwdStr = Encodes.encodeHex(hashPwd).toUpperCase();

        System.out.println(hashPwdStr);
        return hashPwdStr;
    }


    public static void main(String[] args) {
        SysUtils.createSalt();

        getPwdBySalt("123456", "DRTLOWOFHAULO++N1T9L");
    }
}
